layui.config({base: top.basePath+'/static/common/layui/js/'})
    .define(['newAjax','newLayer','laytpl','treeGrid','tableKit'], function(exports) {
    "use strict";

    var $=layui.jquery, ajax=layui.newAjax, laytpl=layui.laytpl,tableKit=layui.tableKit;

    var NewTreeTable = function() {
    	this.target=layui.treeGrid;
        this.needQuery=true;
        this.querys={
            name:undefined
            ,title:'查询区'

            ,elem:undefined
            ,conditions:undefined
            ,editUrl:undefined
            ,noRowButtons:[] // 不需要选择直接点击的按钮
            ,buttons:[]
            ,multiButtons:[]
            ,notUseCommonButton:false
        };
        this.config = tableKit.doConfig({
            elem: "#dataTreeTable" //存放begtable的窗口，必填
            ,height:'full-150'
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'upId'//树形父id字段名称
            ,treeShowName:'name'//以树形式显示的字段
            ,id:"dataTreeTable"//设定容器唯一ID
            ,openColEdit:true //是否打开列编辑模式
        });
    };


    /**
     * @description 查询条件
     * @param {Object} options
     */
    NewTreeTable.prototype.query = function(querys) {
        var that = this;
        tableKit.doQuery(that,querys);
        return that;
    };

    /**
     * @description 渲染
     * @param {Object} options
     */
    NewTreeTable.prototype.render = function(options) {
        var that = this;

        tableKit.doRender(that,options);
        return that;
    };


    NewTreeTable.prototype.search = function() {
        tableKit.doSearch(this);
    };
    NewTreeTable.prototype.clear = function() {
        tableKit.doClear(this);
    };


        /**
     * @description 获取选择的行
     */
    NewTreeTable.prototype.getSelectedRows = function(needSingle) {
        return tableKit.doGetSelectedRows(this,needSingle);
    };
    /** 选中行 之后回调方法，将选中的单行 或 多行回传 */
    NewTreeTable.prototype.callWhenSelectOneRow = function(cb) {
        return tableKit.doCallWhenSelectRows(this,true,cb);
    };
    NewTreeTable.prototype.callWhenSelectRows = function(cb) {
        return tableKit.doCallWhenSelectRows(this,false,cb);
    };

    /**
     * @description 获取选择的行的id数组
     */
    NewTreeTable.prototype.getSelectedRowIds = function() {
        return tableKit.doGetSelectedRowIds(this);
    };
    NewTreeTable.prototype.callWhenSelectRowIds = function(cb) {
        return tableKit.doCallWhenSelectRowIds(this,cb);
    };



    NewTreeTable.prototype.bindSearch= function () {
        var that=this;
        tableKit.doBindSearch(this);
        return that;
    }

    NewTreeTable.prototype.bindModifyFunction= function () {
        var that=this;
        tableKit.doBindModifyFunction(that);
        return that;
    }
        NewTreeTable.prototype.openMixChoose= function () {
            var that=this;
            tableKit.doOpenMixChooseType(that);
            return that;
        }

    NewTreeTable.prototype.openNewLayer=function (title,url) {
        var that=this;
        tableKit.doOpenNewLayer(title,url);
        return that;
    }

    NewTreeTable.prototype.refreshTable=function (param) {
        var that=this;
        that.target.reload(that.config.id,{
            where: param || ''
            ,page: {curr: 1}
        });
        return that;
    }

    NewTreeTable.prototype.openChooseLayerSelectRows=function (title,url,needSingle,callBack,isTreeTable) {
        var that=this;
        layui.newLayer.open({
            title: title,
            content: url,
            btn:['确定'],
            yes:function (index,openLayer) {
                var rows=openLayer.find("iframe")[0].contentWindow.layui[isTreeTable?'newTreeTable':'newTable'].getSelectedRows(needSingle);
                if(!rows)return

                callBack(rows);
                layui.newLayer.close(index);
            }
        });

        return that;
    }

    NewTreeTable.prototype.openChooseLayerSelectIds=function (title,url,callBack,isTreeTable) {
        var that=this;
        layui.newLayer.open({
            title: title,
            content: url,
            btn:['确定'],
            yes:function (index,openLayer) {
                var ids=openLayer.find("iframe")[0].contentWindow.layui[isTreeTable?'newTreeTable':'newTable'].getSelectedRowIds();
                if(!ids)return;

                callBack(index,openLayer,ids);
            }
        });

        return that;
    }


    /**
     * 弹窗 编辑，确定后回调
     * @param title 抬头说明
     * @param fieldArray 弹窗 中 需要 输入的属性
     * @param cb 提交后 需要 回调函数，会将form的所有属性都返回
     * @returns {newTable}
     */
    NewTreeTable.prototype.openEditLayerWithFieldsThenCall=function (title,fieldArray, cb) {
        var that=this;
        tableKit.doOpenEditLayerWithFieldsThenCall(that,title, fieldArray, cb);
        return that;
    }



    function msgErrorTips(msg) {
        layui.newLayer.msg(msg, {
            icon: 5
        });
        return;
    }


    var treeTable=new NewTreeTable();


    exports('newTreeTable', treeTable);
});